<?php

namespace Admin\Models\read;

use Helpers\Main;
use Phalcon\Di;
use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class BaseRead extends Model
{

    const Prefix  = 'read_';
    const Service = 'readDB';

    /**
     * 创建数据库服务
     *
     * create by ck 20170413
     */
    public static function setReadDBService()
    {
        if(!Di::getDefault()->has(self::Service)){
            $readDBConfig = Main::getConfigByName(self::Service);
            Di::getDefault()->setShared(self::Service, function() use ($readDBConfig){
                return new Mysql($readDBConfig);
            });
        }
    }

    /**
     * 初始化软删除
     *
     * create by ck 20170227
     */
    public function initialize()
    {
        self::setReadDBService();
        $this->useDynamicUpdate(true);
        $this->addBehavior(new SoftDelete(
            array(
                'field' => 'status',
                'value' => 2,
            )
        ));
    }

    /**
     * 设置统一表前缀
     *
     * @param $tableName
     * @return string
     *
     * create by ck 20170228
     */
    public function getTableName($tableName)
    {
        return self::Prefix . $tableName;
    }

    /**
     * 创建前记录最后修改时间及操作人
     *
     * @return bool
     *
     * create by ck 20170318
     */
    public function beforeCreate()
    {
        $userId  = Main::getLoginUserInfo('userId');
        $time    = date('Y-m-d H:i:s');
        if(property_exists($this, 'status')){
            $this->writeAttribute('status', 1);
        }
        if(property_exists($this, 'createtime')){
            $this->writeAttribute('createtime', $time);
        }
        if(property_exists($this, 'createuserid')){
            $this->writeAttribute('createuserid', $userId);
        }
        if(property_exists($this, 'updatetime')){
            $this->writeAttribute('updatetime', date('Y-m-d H:i:s'));
        }
        if(property_exists($this, 'updateuserid')){
            $this->writeAttribute('updateuserid', $userId);
        }
        return true;
    }

    /**
     * 更新前记录最后修改时间及操作人
     *
     * @return bool
     *
     * create by ck 20170318
     */
    public function beforeUpdate()
    {
        if(property_exists($this, 'updatetime')){
            $this->writeAttribute('updatetime', date('Y-m-d H:i:s'));
        }
        if(property_exists($this, 'updateuserid')){
            $this->writeAttribute('updateuserid', Main::getLoginUserInfo('userId'));
        }
        return true;
    }

    /**
     * 删除前记录最后修改时间及操作人
     *
     * @return bool
     *
     * create by ck 20170227
     */
    public function beforeDelete()
    {
        if(property_exists($this, 'updatetime')){
            $this->writeAttribute('updateuserid', date('Y-m-d H:i:s'));
        }
        if(property_exists($this, 'updateuserid')){
            $this->writeAttribute('updateuserid', Main::getLoginUserInfo('userId'));
        }
        return true;
    }

}